package com.liuyonggang.generator.util;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.liuyonggang.generator.model.Database;
import com.liuyonggang.generator.model.Table;

/**
 * 代码生成类
 */
public class CodeGenerator {

	private Logger logger = Logger.getLogger(CodeGenerator.class);
	
	//是否去掉模块前面的编号
	public static String prefix = "";
	
	public boolean generator(final String template,final String classDriver, final  String url, final String username, final String password, final String tableName, 
			final String classPackage, final String author, final String contact, final String sourcePath,final String description) {
		
		try {
			Long start = System.currentTimeMillis();
		    // 获取数据库名
		 	String schema = url.substring(url.lastIndexOf("/") + 1);
		    
			// 获取数据库信息
		 	DatabaseInfoOp op = new DatabaseInfoOp(classDriver, url, username, password, schema);
			Database databaseBean = op.getDbInfo();
			
			// 获取该库所有表
			List<Table> tableList = databaseBean.getTableList();
			
			logger.info("---------------start---------------");
			
			/**
			 * 遍历生成代码
			 */
			if(tableList != null && tableList.size() > 0){
				if(StringUtils.isNotBlank(tableName)){
					for (Table table : tableList) {
						if(table.getTableName().equals(tableName)){
							table.setPackageName(classPackage);
							
							Map<String, Object> map = new HashMap<String, Object>();

							map.put("table", table);
							map.put("contact", contact);
							map.put("author", author);
							map.put("description", op.getTableRemark(table.getTableName()));
							map.put("date", StringUtil.date2String());
							map.put("template", template);
							
							String path = sourcePath+File.separator+table.getPackagePath()+ File.separator;

							VelocityInfoOp.generatorCode("model.vm", map, path +"dto", table.getClassName() + "DTO.java");
							VelocityInfoOp.generatorCode("xml.vm", map, path +"dao"+File.separator+"sql", table.getClassName() + ".xml");
							VelocityInfoOp.generatorCode("dao.vm", map, path +"dao", table.getClassName() + "Dao.java");
							VelocityInfoOp.generatorCode("daoImpl.vm", map, path +"dao"+File.separator+"impl", table.getClassName() + "DaoImpl.java");
							VelocityInfoOp.generatorCode("service.vm", map, path +"service", table.getClassName() + "Service.java");
							VelocityInfoOp.generatorCode("serviceImpl.vm", map, path +"service"+File.separator+"impl", table.getClassName() + "ServiceImpl.java");
							VelocityInfoOp.generatorCode("controller.vm", map, path, table.getClassName() + "Controller.java");
							VelocityInfoOp.generatorCode("jsp.vm", map, path, table.getClassNameFirstLower() + ".jsp");
							
							break;
						}
					}
				}else{
					for (Table table : tableList) {
						table.setPackageName(classPackage);
						
						Map<String, Object> map = new HashMap<String, Object>();

						map.put("table", table);
						map.put("contact", contact);
						map.put("author", author);
                        map.put("description", op.getTableRemark(table.getTableName()));
						map.put("date", StringUtil.date2String());
						map.put("template", template);

						String type="upingan",path = sourcePath+File.separator+table.getPackagePath()+ File.separator;;
//						type="cms";
//						type="ojobs";
//						type="codebase";
						type="console";
						
						if("upingan".equals(type)){
							VelocityInfoOp.generatorCode("model.vm", map, path +"dto", table.getClassName() + "DTO.java");
							VelocityInfoOp.generatorCode("xml.vm", map, path +"dao"+File.separator+"sql", table.getClassName() + ".xml");
							VelocityInfoOp.generatorCode("dao.vm", map, path +"dao", table.getClassName() + "Dao.java");
							VelocityInfoOp.generatorCode("daoImpl.vm", map, path +"dao"+File.separator+"impl", table.getClassName() + "DaoImpl.java");
							VelocityInfoOp.generatorCode("service.vm", map, path +"service", table.getClassName() + "Service.java");
							VelocityInfoOp.generatorCode("serviceImpl.vm", map, path +"service"+File.separator+"impl", table.getClassName() + "ServiceImpl.java");
							VelocityInfoOp.generatorCode("controller.vm", map, path, table.getClassName() + "Controller.java");
							VelocityInfoOp.generatorCode("jsp.vm", map, path, table.getClassNameFirstLower() + ".jsp");
						}else if("cms".equals(type)){
							VelocityInfoOp.generatorCode("/cms/model.vm", map, path + "/domain", table.getClassName() + ".java");
							VelocityInfoOp.generatorCode("/cms/dao.vm", map, path + "/dao", table.getClassName() + "Dao.java");
							VelocityInfoOp.generatorCode("/cms/daoImpl.vm", map, path + "/dao", table.getClassName() + "DaoImpl.java");
							VelocityInfoOp.generatorCode("/cms/service.vm", map, path + "/service", table.getClassName() + "Service.java");
							VelocityInfoOp.generatorCode("/cms/serviceImpl.vm", map, path + "/service", table.getClassName() + "ServiceImpl.java");

						}else if("ojobs".equals(type)){
							VelocityInfoOp.generatorCode("/ojobs/model.vm", map, path + "/model", table.getClassName() + ".java");
							VelocityInfoOp.generatorCode("/ojobs/controller.vm", map, path , table.getClassName() + "Controller.java");
							VelocityInfoOp.generatorCode("/ojobs/list.vm", map, path + table.getClassNameFirstLower() ,  "list.html");
							VelocityInfoOp.generatorCode("/ojobs/form.vm", map, path + table.getClassNameFirstLower() ,"form.html");
							
						}else if("codebase".equals(type)){
							VelocityInfoOp.generatorCode("/hubs1/codebase/controller.vm", map, path, table.getClassName() + "Action.java");
							VelocityInfoOp.generatorCode("/hubs1/codebase/list.vm", map, path ,table.getClassNameFirstLower()+".ftl");
							
						}else if("codesystem".equals(type)){
							VelocityInfoOp.generatorCode("/hubs1/codesystem/controller.vm", map, path, table.getClassName() + "Action.java");
							VelocityInfoOp.generatorCode("/hubs1/codebase/list.vm", map, path ,table.getClassNameFirstLower()+".ftl");
						
						}else if("console".equals(type)){
//							if(table.getTableName().equals("sys_appliction")){
								VelocityInfoOp.generatorCode("/console/model.vm", map, path +"entity", table.getClassName() + ".java");
								VelocityInfoOp.generatorCode("/console/dao.vm", map, path +"dao", table.getClassName() + "Dao.java");
								VelocityInfoOp.generatorCode("/console/daoImpl.vm", map, path +"dao"+File.separator+"impl", table.getClassName() + "DaoImpl.java");
								VelocityInfoOp.generatorCode("/console/service.vm", map, path +"service", table.getClassName() + "Service.java");
								VelocityInfoOp.generatorCode("/console/serviceImpl.vm", map, path +"service"+File.separator+"impl", table.getClassName() + "ServiceImpl.java");
								VelocityInfoOp.generatorCode("/console/controller.vm", map, path, table.getClassName() + "Action.java");
								VelocityInfoOp.generatorCode("/console/readme.vm", map, path, "readme-"+table.getClassName()+".md");
//							}
							
						}
						logger.info("表：" + table.getTableName() + "成功");
					}
				}
				
			}
			

			logger.info("--------------- end time：" + (System.currentTimeMillis() - start) + "ms-----");
			logger.info("代码路径：" + sourcePath);
			logger.info("包：" + classPackage);
			logger.info("作者：" + author);
			logger.info("联系方式：" + contact);
			logger.info("------------------------------------");
			return true;
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		return false;
	}
	
}
